package com.fh.controller.ccode.mission;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fh.controller.base.BaseController;
import com.fh.service.ccode.AppCcodeUserService;
import com.fh.service.ccode.MissionService;
import com.fh.util.AppUtil;
import com.fh.util.PageData;

@Controller
@RequestMapping(value = "/appmission")
public class MissionController extends BaseController {

	@Resource(name = "missionService")
	private MissionService missionService;

	@Resource(name = "appCcodeUserService")
	private AppCcodeUserService appCcodeUserService;

	@RequestMapping(value = "/milist")
	@ResponseBody
	public Object getMissionListToHome(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		// 获取传过来的参数
		PageData pd = new PageData();
		pd = this.getPageData();

		int start = (Integer.parseInt(pd.getString("pageNum")) - 1) * Integer.parseInt(pd.getString("pageSize"));
		pd.put("start", start);

		int size = Integer.parseInt(pd.getString("pageSize"));
		pd.put("size", size);

		pd.remove("pageNum");
		pd.remove("pageSize");

		try {
			// 用到pd
			List<PageData> list = missionService.getMissionListHome(pd);

			List<PageData> temp = new ArrayList<PageData>();

			for (int i = 0; i < list.size(); i++) {
				PageData temppd = new PageData();
				Date postTime = (Date) list.get(i).get("POST_TIME");
				Date startTime = (Date) list.get(i).get("START_TIME");
				Date endTime = (Date) list.get(i).get("END_TIME");
				
				// JDK 提供的类
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

				temppd.put("publishTime", df.format(postTime));
				temppd.put("endDate", df.format(endTime));
				temppd.put("id", list.get(i).get("MISSION_ID") + "");
				temppd.put("title", list.get(i).getString("MISSION_TITLE"));
				temppd.put("description", list.get(i).getString("MISSION_DESCRIPTION"));
				temppd.put("money", list.get(i).get("PRIZE") + "");
				temppd.put("posterID", list.get(i).get("POSTER_ID") + "");
				temppd.put("startDate", df.format(startTime));
				temppd.put("className", list.get(i).getString("CLASS_NAME"));
				
				temppd.put("joinNum", missionService.getMissionJoinNum(temppd).get("JOIN_NUM") + "");

				temp.add(temppd);

			}

			map.put("missionList", temp);
			map.put("code", "T");
			map.put("msg", "获取任务列表成功");

		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取任务列表失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/addmission")
	@ResponseBody
	public Object addMission(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");

		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		PageData tempPD = appCcodeUserService.getInfoByToken(pd);
		pd.put("posterID", tempPD.get("C_USER_ID"));

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String now = df.format(new Date());
		pd.put("postTime", now);

		if (tempPD.getString("ROLE").equals("ENTERPRISE")) {
			missionService.addMission(pd);
			map.put("code", "T");
			map.put("msg", "添加成功");
		} else {
			map.put("code", "F");
			map.put("msg", "没有权限添加任务");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/getclass")
	@ResponseBody
	public Object getClass(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		try {
			List<PageData> list = missionService.getClassifications(pd);

			List<PageData> temp = new ArrayList<PageData>();
			for (int i = 0; i < list.size(); i++) {
				PageData tempPD = new PageData();
				tempPD.put("value", list.get(i).get("CLASS_ID"));
				tempPD.put("name", list.get(i).getString("CLASS_NAME"));

				temp.add(tempPD);
			}

			map.put("classlist", temp);
			map.put("code", "T");
			map.put("msg", "获取任务分类成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取任务分类失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/getarea")
	@ResponseBody
	public Object getAreas(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		try {
			List<PageData> list = missionService.getAreas(pd);
			List<PageData> temp = new ArrayList<PageData>();
			for (int i = 0; i < list.size(); i++) {
				PageData tempPD = new PageData();
				tempPD.put("value", list.get(i).get("CITY_ID"));
				tempPD.put("name", list.get(i).getString("CITY_NAME"));

				temp.add(tempPD);
			}

			map.put("arealist", temp);
			map.put("code", "T");
			map.put("msg", "获取任务地区成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取任务地区失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/getonemission")
	@ResponseBody
	public Object getOneMission(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();
		PageData pd = new PageData();
		pd = this.getPageData();

		if (missionService.findMissionByID(pd) != null) {
			pd = missionService.findMissionByID(pd);
			PageData temp = new PageData();

			Date postTime = (Date) pd.get("POST_TIME");
			Date startTime = (Date) pd.get("START_TIME");
			Date endTime = (Date) pd.get("END_TIME");

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

			temp.put("title", pd.getString("MISSION_TITLE"));
			temp.put("publishTime", df.format(postTime));
			temp.put("className", pd.getString("CLASS_NAME"));
			temp.put("writer", pd.getString("NAME"));
			temp.put("content", pd.getString("MISSION_DESCRIPTION"));
			temp.put("startTime", df.format(startTime));
			temp.put("endTime", df.format(endTime));

			map.put("data", temp);
			map.put("code", "T");
			map.put("msg", "获取单任务成功");
		} else {
			map.put("code", "F");
			map.put("msg", "获取单任务失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/delMission")
	@ResponseBody
	public Object delMission(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();

		PageData pd = new PageData();
		pd = this.getPageData();

		try {
			String poster = missionService.findMissionByID(pd).get("POSTER_ID") + "";
			String token = appCcodeUserService.getInfoByToken(pd).get("C_USER_ID") + "";
			if(poster.equals(token))
			{
				missionService.delMission(pd);
				map.put("code", "T");
				map.put("msg", "删除任务成功");
			}else{
				map.put("code", "F");
				map.put("msg", "没有权限删除任务");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "删除任务失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}

	@RequestMapping(value = "/addMiComment")
	@ResponseBody
	public Object addMiComment(HttpServletRequest request, HttpServletResponse response) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = new HashMap<String, Object>();

		PageData pd = new PageData();
		pd = this.getPageData();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		pd.put("posttime", df.format(new Date()));

		try {
			PageData tempPD = appCcodeUserService.getInfoByToken(pd);
			pd.put("userID", tempPD.get("C_USER_ID"));
			
			missionService.addComment(pd);
			map.put("code", "T");
			map.put("msg", "添加评论成功");
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "添加评论失败");
		}

		return AppUtil.returnObject(new PageData(), map);
	}
	
	@RequestMapping(value="/getMiComments")
	@ResponseBody
	public Object getMiComments(HttpServletRequest request,HttpServletResponse response)
	{
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> map = new HashMap<String,Object>();
		
		PageData pd = new PageData();
		pd = this.getPageData();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		
		try{
			List<PageData> list = missionService.getMiComments(pd);
			List<PageData> tempList = new ArrayList<PageData>();
			
			for(int i = 0; i < list.size(); i++)
			{
				PageData tempPD = new PageData();
				tempPD.put("avatarUrl", list.get(i).getString("HEAD_URL"));
				tempPD.put("name", list.get(i).getString("NAME"));
				tempPD.put("time", df.format(list.get(i).get("MI_COMMENT_POSTTIME")));
				tempPD.put("content", list.get(i).getString("MI_COMMENT_CONTENT"));
				
				tempList.add(tempPD);
			}
			map.put("data", tempList);
			map.put("code", "T");
			map.put("msg", "获取任务评论成功");
			
		}catch(Exception e)
		{
			e.printStackTrace();
			map.put("code", "F");
			map.put("msg", "获取任务评论失败");
		}
		
		return AppUtil.returnObject(new PageData(), map);
	}

}
